package com.company.nowcoder;
import java.util.*;
public class 牛妹的礼物 {

    public static void main(String[] args) {
        System.out.println(selectPresent(new int[][]{{1, 2, 3}, {2, 3, 4}}));
    }


    public static int selectPresent (int[][] presentVolumn) {
        // write code here
        if (presentVolumn.length == 0) return 0;
        int m = presentVolumn.length,n = presentVolumn[0].length;
        if (m == 0 || n == 0) return 0;
        int[][] dp = new int[m][n];
        dp[0][0] = presentVolumn[0][0];
        for (int i = 1; i < m; i++) {
            dp[i][0] = dp[i - 1][0] + presentVolumn[i][0];
        }
        for (int i = 1; i < n; i++) {
            dp[0][i] = dp[0][i - 1] + presentVolumn[0][i];
        }

        for (int i = 1 ; i < m; i++) {
            for (int j = 1; j < n; j++) {
                int value01 = dp[i - 1][j];
                int value02 = dp[i][j - 1];
                int value03 = dp[i - 1][j - 1];
                dp[i][j] = Math.min(Math.min(value01,value02),value03) + presentVolumn[i][j];
            }
        }

        return dp[m - 1][n - 1];
    }

}
